package org.iridium.allcomments.buzz;

import java.util.List;

import org.iridium.allcomments.common.Constants;
import org.iridium.allcomments.dto.TagDTO;
import org.iridium.allcomments.exception.AllCommentsException;
import org.iridium.allcomments.persistance.CommentsDAO;
import org.iridium.allcomments.persistance.CommentsDAOImpl;
import org.iridium.allcomments.persistance.UserTagDAO;
import org.iridium.allcomments.persistance.UserTagDAOImpl;

/**
 * 
 * @author iridium
 * 
 */
public class TagBuzz {
	
	/**
	 * if a tag already exists
	 * 
	 * @param tag
	 * @return
	 */
	public boolean hasTag(TagDTO tag) {
		UserTagDAO tagDao = new UserTagDAOImpl();
		return tagDao.existsTag(tag.getUserId(), tag.getTag());
	}
	
	/**
	 * add a tag in table 'user_tag'
	 * 
	 * @param tag
	 */
	public void addTag(TagDTO tag) {
		UserTagDAO tagDao = new UserTagDAOImpl();
		tagDao.addItem(tag.getUserId(), tag.getTag(), tag.getVisibility());
	}

	/**
	 * remove a tag of one user
	 * 
	 * @param tag
	 * @throws AllCommentsException 
	 */
	public void removeTag(TagDTO tag) throws AllCommentsException {
		long userId = tag.getUserId();
		String tagContent = tag.getTag();
		if (!this.hasTag(tag)) {
			throw new AllCommentsException("This tag(" + tag.getUserId() + ", "
					+ tag.getTag() + ") does not exist.");
		} else {
			UserTagDAO tagDao = new UserTagDAOImpl();
			tagDao.deleteTag(userId, tagContent);
		}

		CommentsDAO commDao = new CommentsDAOImpl();
		List<String[]> commTags = commDao.queryTagsFromOne(userId);
		for (String[] elem : commTags) {
			String[] tags = elem[1].split("\\|\\|");
			StringBuffer sbNewTags = new StringBuffer();
			for (String aTag : tags) {
				if (!aTag.equals(tagContent)) {
					sbNewTags.append(aTag).append(Constants.TAG_SEPARATOR);
				}
			}

			for (String aTag : tags) {
				if (aTag.equals(tagContent)) {
					commDao.updateTags(elem[0], sbNewTags.toString());
				}
			}
		}
	}

	/**
	 * remove all tags of one user
	 * 
	 * @param userId
	 */
	public void removeTags(long userId) {
		UserTagDAO tagDao = new UserTagDAOImpl();
		tagDao.deleteTag(userId);
		CommentsDAO commDao = new CommentsDAOImpl();
		commDao.delTagsFromOne(userId);
	}

	/**
	 * modify a tack
	 * 
	 * @param oldTag
	 * @param newTag
	 * @throws AllCommentsException 
	 */
	public void modifyTag(TagDTO oldTag, TagDTO newTag)
			throws AllCommentsException {
		String oldTagContent = oldTag.getTag();
		String newTagContent = newTag.getTag();
		if (oldTag.getUserId() != newTag.getUserId()) {
			throw new AllCommentsException("One can only modify his own tags.");
		}
		
		// add tag to table 'user_tag'
		addTag(newTag);

		// add new tag to the comments in table 'comments'
		CommentsDAO commDao = new CommentsDAOImpl();
		List<String[]> commTags = commDao.queryTagsFromOne(oldTag.getUserId());
		for (String[] elem : commTags) {
			String[] tags = elem[1].split("\\|\\|");
			StringBuffer sbNewTags = new StringBuffer();
			for (String aTag : tags) {
				if (!aTag.equals(oldTagContent)) {
					sbNewTags.append(aTag).append(Constants.TAG_SEPARATOR);
				}
			}
			sbNewTags.append(newTagContent).append(Constants.TAG_SEPARATOR);

			for (String aTag : tags) {
				if (aTag.equals(oldTagContent)) {
					commDao.updateTags(elem[0], sbNewTags.toString());
				}
			}
		}
		
		removeTag(oldTag);
	}
}
